package com.ming.closeto.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;

import com.ming.closeto.base.BasePageAdapter;
import com.ming.closeto.MainActivity;
import com.ming.closeto.R;
import com.ming.closeto.utils.LogUtils;
import com.ming.closeto.utils.ScreenUitls;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity implements ViewPager.OnPageChangeListener, View.OnTouchListener {

    private ViewPager mViewPager;

    /**
     * ViewPager滑动
     * ViewPager尾页滑动 右滑监听
     */

    private View viewOne, viewTwo, viewThree;
    private List<View> mList = new ArrayList<>();

    // 当前的Item
    private int currentItem;

    private int startX, endX = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initData();
        initView();
    }

    private void initData() {
        // 加载3个view
        viewOne = View.inflate(this, R.layout.layout_guide_view_one_item, null);
        viewTwo = View.inflate(this, R.layout.layout_guide_view_two_item, null);
        viewThree = View.inflate(this, R.layout.layout_guide_view_three_item, null);

        mList.add(viewOne);
        mList.add(viewTwo);
        mList.add(viewThree);
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);


        // 设置适配器
        mViewPager.setAdapter(new BasePageAdapter(mList));

        // 监听滑动事件
        mViewPager.addOnPageChangeListener(this);

        // 监听触摸事件
        mViewPager.setOnTouchListener(this);


    }

    @Override
    public void onPageScrolled(int position, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        // 状态改变
        currentItem = position;
    }

    @Override
    public void onPageScrollStateChanged(int position) {

    }

    /**
     * 触摸事件
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        /**
         *  按下  抬起  移动
         */
        switch (event.getAction()) {
            // 按下
            case MotionEvent.ACTION_DOWN:
                startX = (int) event.getX();    // getX 以空间为轴   getRawX 以全屏幕
                break;
            // 抬起
            case MotionEvent.ACTION_UP:
                endX = (int) event.getX();

                // 判断是否时最后一页
                if (currentItem == (mList.size() - 1)) {
                    if (startX - endX > 0) { // 说明有滑动距离
                        if (startX - endX >= ScreenUitls.width / 4) { // 滑动超过屏幕 1/4  就认为时左滑
                            LogUtils.i("进入主页");
                            startActivity(new Intent(this, MainActivity.class));
                            finish();
                        }
                    }
                }

                break;
        }

        return false;
    }

}
